<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Db;

class Passenger Extends Base
{
	
	protected $id = 'passenger_id';
	protected $table = 'passenger';
//	protected $join = ['company cu','cu.company_id = m.company_id','left'];
	protected $joins = [
        ['company cu','cu.company_id = m.company_id','left'],
        ['user u','u.user_id = m.user_id','left']
    ];
	protected $field = 'm.*,cu.company_name,cu.quota,u.username';
	
	
	protected function parseData($data) {
		
		$id = $data['user_id'];
//		if($data['password']) {
			
			$user = [];
			//查询关联用户
			$repeat = Db::name('user')->where('mobile',$data['phone'])->whereNull('delete_time')->find();
			$user['role_id'] = explode(',', $data['role_id']);
			
			if(!in_array(2,$user['role_id'])) {
				$user['role_id'][] = 2;
				$user['role_id'] = implode(',',array_filter(array_unique(($user['role_id']))));
			}
			
//			halt($user);
			//编辑
			if($id) {
				
				if($data['password']) {
					$user['salt'] = substr(uniqid(), -6);#新盐
					$user['password'] = CorePwd($data['password'],$user['salt']);
					Db::name('user')->where('user_id',$id)->update($user);
				}
				
			} else {
				
				$user['role_id']  = 2;
				$user['username'] = $data['phone'];
				$user['mobile'] = $data['phone'];
				
				$user['salt'] = substr(uniqid(), -6);#新盐
				$user['password'] = CorePwd('111111',$user['salt']);

				if($repeat) {
					return $this->error('手机已存在，用户添加失败，请修改手机');
				}
				//区域
				$user['area_id'] = Db::name('company')->where('company_id',$data['company_id'])->value('area_id');
				$data['user_id'] = Db::name('user')->insertGetId($user);
			}
//		} else {
//			if(empty($data['user_id'])) {
//				return $this->error('新增用户，必须填密码');
//			}
//		}
		
		unset($data['password']);
		$data['update_time'] = time();
		
		return $data;
		
	}
    
    //删除 
	public function delete() {
		
		$id = input('post.id/a');
		
		if($id) {
            //查询用户ID
            $uids = Db::name($this->table)->whereIn($this->id,$id)->column('user_id');
            $rslt = Db::name('user')->whereIn('user_id',$uids)->setField('delete_time',time());
            
//			if(is_array($id)) $id = implode(',',$id);
            if($rslt) {
                $rslt = Db::name($this->table)->whereIn($this->id,$id)->setField('delete_time',time());   
            } else {
                Db::name('user')->whereIn('user_id',$uids)->setField('delete_time',null);
            }
			
		}
		
		return $this->res($rslt);
		
	}

	protected function options($item = []) {
		
		$used_types = config('base.used_types');
		$this->assign('company_types',$used_types);
		
		return $item;
	}

    //下载excel表格
    public function outexcel() {

        $P = input('param.');
        $where = [];

        $P['company_id'] && $where['company_id'] = ['=',$P['company_id']];
        $P['name'] && $where['name'] = ['LIKE',"%{$P['name']}%"];
        $P['phone'] && $where['phone'] = ['LIKE',"%{$P['phone']}%"];
        $P['is_check'] && $where['is_check'] = ['=',$P['is_check']];

        $passenger_return = Db::name('passenger')->where($where)->whereNull('delete_time')->field('name,phone,birthday,age,sex,occupation,source,id_number,unit,department_id,live_number,province,city,county,is_check')->order('update_time desc')->select();
        $department_return = Db::name('department')->where('')->whereNull('delete_time')->field('department_id,name')->select();

        //地址的json 不要展开
        $addressJson = (new Passengeraddressjson())->addressJson();

        if($passenger_return) {
            echo "<table border='1'>";
            //标题
            $title = array("姓名","手机号","出生年月","年龄","性别","职业","来源","身份证","公司/单位","部门","居住证","省","市","县","审核员");
            //文件名
            $filename = '乘客管理导出报表';
            header("Content-type:application/octet-stream");
            header("Accept-Ranges:bytes");
            header("Content-type:application/vnd.ms-excel");
            header("Content-Disposition:attachment;filename=".$filename.".xls");
            header("Pragma: no-cache");
            header("Expires: 0");
            if (!empty($title)) {
                foreach ($title as $k => $v) {
                    $title[$k] = iconv("UTF-8", "GB2312", "<td>".$title[$k]."</td>");
                    echo $title[$k];
                }
                echo "</tr>";
            }
            if (!empty($passenger_return)) {
                foreach ($passenger_return as $key => $val) {
                    echo "<tr>";

                    if ($passenger_return[$key]['sex']=='1'){
                        $passenger_return[$key]['sex'] = "女";
                    }
                    else{
                        $passenger_return[$key]['sex'] = "男";
                    }

                    if ($passenger_return[$key]['is_check']=='1'){
                        $passenger_return[$key]['is_check'] = "审核员";
                    }
                    else{
                        $passenger_return[$key]['is_check'] = "非审核员";
                    }

                    if ($passenger_return[$key]['source']=='0'){
                        $passenger_return[$key]['source'] = "公务员";
                    }
                    elseif($passenger_return[$key]['source']=='1'){
                        $passenger_return[$key]['source'] = "企业员工";
                    }
                    elseif($passenger_return[$key]['source']=='2'){
                        $passenger_return[$key]['source'] = "社会人员";
                    }

                    foreach ($department_return as $key2 => $val2) {
                        if($passenger_return[$key]['department_id'] == $department_return[$key2]['department_id']){
                            $passenger_return[$key]['department_id'] = $department_return[$key2]['name'];
                        }
                    }

                    $province = $passenger_return[$key]['province'];
                    $city = $passenger_return[$key]['city'];
                    $county = $passenger_return[$key]['county'];
                    $passenger_return[$key]['province'] = $addressJson[$province]['name'];
                    $passenger_return[$key]['city'] = $addressJson[$province]['city'][$city]['name'];
                    $passenger_return[$key]['county'] = $addressJson[$province]['city'][$city]['district'][$county]['name'];

                    foreach ($val as $ck => $cv) {
                        $passenger_return[$key][$ck] = iconv("UTF-8", "GB2312", "<td style=\"vnd.ms-excel.numberformat:@\">".$passenger_return[$key][$ck]."</td>");
                        echo $passenger_return[$key][$ck];
                    }
                    echo "</tr>";
                }
            }
        }
        else {
            $this->error('无数据');
        }
    }

    //重置密码
    public function resetpassword(){
        $P = input('param.');
        $where = [];

        $P['passenger_id'] && $where['passenger_id'] = ['=',$P['passenger_id']];

        $passenger_return = Db::name('passenger')->where($where)->find();
        $user_return = Db::name('user')->where('user_id',$passenger_return['user_id'])->find();
        $user_return = Db::name('user')->where('user_id',$passenger_return['user_id'])->update(['password' => CorePwd(1234567,$user_return['salt'])]);
        return $this->success('重置密码成功,新密码:1234567');
//        print_r($passenger_return['user_id']);

    }
}	
